const {remote, clipboard, ipcRenderer } = require('electron');

var arrled1 =["#First_full_4","#First_full_3","#First_full_2","#First_full_1","#First_stop_0","#First_slow_1","#First_slow_2","#First_slow_3","#First_slow_4"];
var arrled2 =["#Second_full_4","#Second_full_3","#Second_full_2","#Second_full_1","#Second_stop_0","#Second_slow_1","#Second_slow_2","#Second_slow_3","#Second_slow_4"];
var power1flage=0;


var power2flage=0;
var B="4";
var S="4";
//只给两个力度 一前一后  power开不开 你都不用管 "CT": B,4,S,4"} [0--8]
var send_data_udp={"id":"8","CT":"B,4,S,4"}; 
function init()
{
	power1flage=0;
	power2flage=0; 
	 B="4";
	 S="4";
	send_data_udp={"id":"8","CT":"B,4,S,4"}; 
	$("#ALARM_ACK").css("background-color", arr4[1]);
	$("#First_power").css("background-color", arr4[1]);
	$("#Second_power").css("background-color", arr4[1]);
	for(var i=0;i<9;i++){
	$(arrled1[i]).css("background-color", arr4[0]);
	}
	for(var i=0;i<9;i++){
	$(arrled2[i]).css("background-color", arr4[0]);
	}
}
	

window.onload =init;
//从李明月得到数据
 	ipcRenderer.on('message-from-main', (event, arg) => {
	
	   console.log("message from main");
	 
	if(JSON.parse(arg).CT =="100"){ //数据断开连接 初始化
		init();
		add_bow(0);
		add_stren(0);
	}
	else{//进行进度条显示
		console.log(JSON.parse(arg).CT);
		var arr = JSON.parse(arg).CT;
		add_bow((arr.split(","))[1]);
		add_stren((arr.split(","))[3]);
	}
	
		
	  }) 
	  //-160~160
	  function dangweivalue(value){
		  var returnvalue=0;
		  if((-200<value)&&(value<-130))returnvalue=-4;
		  if((-130<value)&&(value<-95))returnvalue=-3;
		  if((-95<value)&&(value<-60))returnvalue=-2;
		  if((-60<value)&&(value<-25))returnvalue=-1;
		   if(value>25)returnvalue=1;
		   if(value>60)returnvalue=2;
		   if(value>95)returnvalue=3;
		   if(value>130)returnvalue=4;
		   if((value>-25)&&(value<25))returnvalue=0;
		   return returnvalue;
		  
		  
	  }
	ipcRenderer.on('marh-data', (event, arg) => {

	arg.FrontT=parseInt(-(arg.FrontT));
	arg.BackT=parseInt((arg.BackT));
	arg.FrontT=dangweivalue(arg.FrontT);
	arg.BackT=dangweivalue(arg.BackT);
	
	
	arg.FrontT=arg.FrontT+4;
	arg.BackT=arg.BackT+4;
	if(arg.FrontT>=8){
		arg.FrontT=8;
	}
	if(arg.FrontT<=0){
		arg.FrontT=0;
	}
	if(arg.BackT>=8){
		arg.BackT=8;
	}
	if(arg.BackT<=0){
		arg.BackT=0;
	}
	 
	   if(power1flage == 0)
	   { 
		   B= arg.FrontT;
		   //赋值（硬件）
	   send_data_udp.CT="B,"+B+",S,"+S;
	   	//发送	
	   	ipcRenderer.send('megfromwhere', {
	   		 send_data_udp
	   })
		}
		
		if(power2flage == 0)
		{ 
		  S= arg.BackT;
				   //赋值（硬件）
		send_data_udp.CT="B,"+B+",S,"+S;
			//发送	
			ipcRenderer.send('megfromwhere', {
				 send_data_udp
		})
		} 
		
		
	  }) 
// panel 1 2 value 012345678

   ipcRenderer.on('text', (event, arg) => {
       if(arg=="CHN")
       	  onepanel8("CHN");
       else{
       	  onepanel8("Eng");
       }
       })



function buttonClick(panel,value){
	
	if(power1flage == 1)
	{
		
		//发送数据
		if(panel == "1")
		{
		B=value;
		send_data_udp.CT="B,"+B+",S,"+S;
			//发送	
			ipcRenderer.send('megfromwhere', {
			send_data_udp 
			})
			
			for(var i=0;i<9;i++)
			{
				$(arrled1[i]).css("background-color", arr4[1]);
			}
			value=parseInt(value);
			if(value<5)
			$(arrled1[value]).css("background-color", arr4[3]);
			else
			$(arrled1[value]).css("background-color", arr4[2]);
		}
	}
	
	if(power2flage == 1)
	{
		if(panel == "2")
		{
			S=value;
		send_data_udp.CT="B,"+B+",S,"+S;
			//发送	
			ipcRenderer.send('megfromwhere', {
			send_data_udp 
			})
		for(var i=0;i<9;i++)
		{
			$(arrled2[i]).css("background-color", arr4[1]);
		}
		value=parseInt(value);
		if(value<5)
		$(arrled2[value]).css("background-color", arr4[3]);
		else
		$(arrled2[value]).css("background-color", arr4[2]);
		}
	}
}
	  

	  
	  
	  
function power1(){


	if(power1flage==0){
		power1flage=1;
		B='4';
		send_data_udp.CT="B,"+B+",S,"+S;
			//发送	
			ipcRenderer.send('megfromwhere', {
				 send_data_udp
		})
		$("#First_power").css("background-color", arr4[2]);
		for(var i=0;i<9;i++){
		
		$(arrled1[i]).css("background-color", arr4[1]);
		}
		$(arrled1[4]).css("background-color", arr4[3]);
		return;
	}
	if(power1flage==1){
		power1flage=0;
		$("#First_power").css("background-color", arr4[1]);
		for(var i=0;i<9;i++){
		$(arrled1[i]).css("background-color", arr4[0]);
		}
		return;
	}
	
}

function power2(){


	if(power2flage==0){
		power2flage=1;
		S='4';
		send_data_udp.CT="B,"+B+",S,"+S;
			//发送	
			ipcRenderer.send('megfromwhere', {
				 send_data_udp
		})
		$("#Second_power").css("background-color", arr4[2]);
		for(var i=0;i<9;i++){
		
		$(arrled2[i]).css("background-color", arr4[1]);
		}
			$(arrled2[4]).css("background-color", arr4[3]);
		return;
	}
	if(power2flage==1){
		power2flage=0;
		$("#Second_power").css("background-color", arr4[1]);
		
		for(var i=0;i<9;i++){
		$(arrled2[i]).css("background-color", arr4[0]);
		}
		return;
	}
	
	
}
